package com.itheima.core.utils;

import com.itheima.core.domain.Configuration;
import com.itheima.core.domain.Mapper;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author slstart
 * @create 2021-05-04 14:25
 */
public class XMLConfigBuilder {
    /**
     * 解析 传入的配置文件字节流 生成Configuration对象,并返回
     *
     * @param is
     * @return
     */
    public static Configuration buildConfiguration(InputStream is) throws DocumentException {
        Configuration cfg = new Configuration();
        //1.获取文件解析对象
        SAXReader saxReader = new SAXReader();
        //2.加载配置文件,生成文档对象
        Document document = saxReader.read(is);
        //3.获取根标签
        Element rootElement = document.getRootElement();
        //4.全文搜索获取每一个property标签
        List<Element> list = rootElement.selectNodes("//property");
        //5.遍历property标签,获取连接数据库的基本信息
        for (Element element : list) {
            //获取property标签中的name属性值
            String name = element.attributeValue("name");
            //获取value属性值
            String value = element.attributeValue("value");
            if ("driver".equals(name)) {
                cfg.setDriver(value);
            } else if ("url".equals(name)) {
                cfg.setUrl(value);
            } else if ("username".equals(name)) {
                cfg.setUsername(value);
            } else if ("password".equals(name)){
                cfg.setPassword(value);
            }
        }
        //--------------------------------------------------------
        //提前创建实体,用于封装数据
        Map<String, Mapper> mappers = new HashMap<String, Mapper>();


        //1.搜索mappers标签
        Element mappersElement = rootElement.element("mappers");
        //2.获取所有mapper标签,一个mapper代表一个映射文件
        List<Element> list2 = mappersElement.elements("mapper");
        //3.遍历所有映射文件
        for (Element mapperElement : list2) {
            //4.读取Sql映射文件,生成文档对象
            String path = mapperElement.attributeValue("resource");
            InputStream in = XMLConfigBuilder.class.getClassLoader().getResourceAsStream(path);
            Document xmlDocument = saxReader.read(in);
            //5.获取映射文件的根标签,mapper
            Element rootElement1 = xmlDocument.getRootElement();
            //6.获取命名空间
            String namespace = rootElement1.attributeValue("namespace");
            //7.获取所有select标签
            List<Element> selectElements = rootElement1.elements("select");

            for (Element selectElement : selectElements) {
                Mapper mapper = new Mapper();
                //8.获取每一个select标签的id属性值
                String id = selectElement.attributeValue("id");
                //9.获取每一个select标签的结果类型值
                String resultType = selectElement.attributeValue("resultType");
                //10.获取每一个select标签的SQL语句
                String sql = selectElement.getText();
                mapper.setResultType(resultType);
                mapper.setSql(sql);
                mappers.put(namespace+"."+id,mapper);
            }

        }

        cfg.setMappers(mappers);

        return cfg;
    }
}
